package ts.group.webapp.market.controller.web;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ts.group.webapp.market.common.core.util.Constants;
import ts.group.webapp.market.common.core.util.ResponseData;
import ts.group.webapp.market.common.core.web.controller.BaseController;
import ts.group.webapp.market.model.TDistributor;
import ts.group.webapp.market.model.TShopInfo;
import ts.group.webapp.market.service.IDistService;
import ts.group.webapp.market.service.IShopService;

import com.starit.common.dao.support.Pagination;
import com.starit.common.dao.support.PaginationRequest;

@Controller
@RequestMapping("/dist")
public class DistController extends BaseController{
	
	@Autowired
	private IDistService	IDistService; 
	
	@Autowired
	private IShopService IShopService;
	
	
	/**
	 * 新增经销商，只接受POST请求
	 */
	@RequestMapping(value = "/insertTdist", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData insertTdist(TDistributor dist) throws Exception {
		List<TDistributor> distList = IDistService.getDistributorByCode(dist.getDistributorCode());
		if(!distList.isEmpty()){
			return new ResponseData(true,"经销商编号重复");
		}
		List<TDistributor> distSponsorList = IDistService.getDistributorByCode(dist.getSponsorCode());
		if(distSponsorList.isEmpty()){
			return new ResponseData(true,"上级经销商编号不正确");
		}
		TDistributor sponsor = distSponsorList.get(0);
		dist.setSponsorId(sponsor.getId());	/*上级id*/
		dist.setSponsor_Name(sponsor.getDistributorName()); /*上级名称*/
		dist.setFloors(sponsor.getFloors()+1); /*层级*/
		dist.setRankId(102001L); /*1星*/
		dist.setRankId_Name("一星");
		dist.setCreateTime(new Date());
		TShopInfo s = IShopService.getShopById(dist.getShopId());
		dist.setShop_Name(s.getShopName());
		dist.setShop_Code(s.getShopCode());
		IDistService.createDistributor(dist);
		return ResponseData.SUCCESS_NO_DATA;
	}
	
	
	/**
	 * 更新经销商, 只接受POST请求
	 */
	@RequestMapping(value = "/updateTdist", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData updateTdist(TDistributor dist) {
		List<TDistributor> sponsorList = IDistService.getDistributorByCode(dist.getSponsorCode());
		if(sponsorList.isEmpty()){
			return new ResponseData(true,"上级编号不正确");
		}
		
		TDistributor old = IDistService.getDistributorById(dist.getId());
		if(!old.getSponsorCode().equals(dist.getSponsorCode())){		/*判断上级编号是否更新*/
			old.setSponsorCode(dist.getSponsorCode());
			old.setSponsor_Name(sponsorList.get(0).getDistributorName());
			old.setSponsorId(sponsorList.get(0).getId());
		}
		  
		if(!old.getShopId().equals(dist.getShopId())){ 	/*判断店铺是否更新*/
			TShopInfo newShop = IShopService.getShopById(dist.getShopId());
			old.setShopId(dist.getShopId());
			old.setShop_Name(newShop.getShopName());
			old.setShop_Code(newShop.getShopCode());
		}
		
		old.setFloors(sponsorList.get(0).getFloors()+1);
		old.setAddress(dist.getAddress());
		IDistService.updateDistributor(old);
		return ResponseData.SUCCESS_NO_DATA;
	}
	
	/**
	 * 批量删除经销商, 只接受POST请求
	 */
	@RequestMapping(value = "/removeTdist", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData removeTdist(Long[] ids) {
		for(Long id : ids){
			IDistService.removeDistributor(id);
		}
		return ResponseData.SUCCESS_NO_DATA;
	}

	
	/**
	 * 分页查询经销商
	 */
	@RequestMapping("/pageQueryTdist")
	@ResponseBody
	public Pagination<TDistributor> pageQueryTdist(
			@RequestParam("start") int startIndex,
			@RequestParam("limit") int pageSize, TDistributor dist,
			@RequestParam(required = false) String sort,
			@RequestParam(required = false) String direction){
		
		PaginationRequest<TDistributor> pageRequest = new PaginationRequest<TDistributor>(startIndex, pageSize);
		if(StringUtils.hasText(sort) && StringUtils.hasText(direction)){
			pageRequest.addOrder(sort, direction);
		}
		if(StringUtils.hasText(dist.getDistributorName())){  /*编号模糊查询*/
			pageRequest.addLikeCondition("distributorName", dist.getDistributorName());
		}
		if(StringUtils.hasText(dist.getDistributorCode())){  /*编号模糊查询*/
			pageRequest.addLikeCondition("distributorCode", dist.getDistributorCode());
		}
		if(StringUtils.hasText(dist.getSponsorCode())){	/*上级编号模糊查寻*/
			pageRequest.addLikeCondition("sponsorCode", dist.getSponsorCode());
		}
		if(null!= dist.getShopId()){	/*店铺查询*/
			pageRequest.addCondition("shopId", dist.getShopId());
		}
		if(null!=dist.getRankId()){	/*职级查询*/
			pageRequest.addCondition("rankId", dist.getRankId());
		}
		Pagination<TDistributor> page = IDistService.findAllForPage(pageRequest);
		
		/*返回结果集屏蔽掉顶级经销商的回显*/
		int  rootPos = -1;
		
		for(TDistributor r :page.getResult()){
			if(r.getId() == Constants._toplev_dist_id){ 
				rootPos = page.getResult().indexOf(r);
				break;
			}
		}
		if(rootPos !=-1){ 
			page.getResult().remove(rootPos);
		}
		return page ;
	}
}
